<?php
namespace app\service;

use Endroid\QrCode\QrCode;
use AliPay\Wap;
use Alipay\Web;
use Alipay\Scan;
use Alipay\Pos;
use Alipay\Bill;

/*
 * 支付宝
 * @author Mateng <Index.phpIndex.php>
 */
class Alipay
{
    public $message = '';
    public $state = 234;
    private $orderTypeMap = [
        '实物', '虚拟', '聚惠卡'
    ];
    /*
     * wap支付
     * @link wx-demo-jsapi
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function wap($info)
    {
        // 组装参数，可以参考官方商户文档
        $options = [
            'out_trade_no' => $info['id'], // 商户订单号
            'total_amount' => floatval($info['pay_fee']),    // 支付金额
            'subject'      => isset($info['body']) ? $info['body'] : '商品', // 支付订单描述
        ];

        try {
            // 实例支付对象
            $pay = new Wap(config('alipay.'));
            // 参考链接：https://docs.open.alipay.com/api_1/alipay.trade.wap.pay
            return $pay->apply($options);
        } catch (Exception $e) {
            // 异常处理
            $this->message = $e->getMessage();
        }

        return false;
    }

    /*
     * web支付
     * @link wx-demo-jsapi
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function web($info)
    {
        // 组装参数，可以参考官方商户文档
        $options = [
            'out_trade_no' => $info['id'], // 商户订单号
            'total_amount' => floatval($info['pay_fee']),    // 支付金额
            'subject'      => $info['body'], // 支付订单描述
        ];

        try {
            // 实例支付对象
            $pay = new Web(config('alipay.'));
            // 参考链接：https://docs.open.alipay.com/api_1/alipay.trade.wap.pay
            return $pay->apply($options);
        } catch (Exception $e) {
            // 异常处理
            $this->message = $e->getMessage();
        }

        return false;
    }

    /*
     * 支付通知接收处理
     * @return string
     * @throws \WeChat\Exceptions\InvalidResponseException
     */
    public function notify()
    {
        try{
            $wepay = new Pay(config('wechat.'));
            $response = $wepay->getNotify();
            p($response);
            if($response['return_code'] === 'SUCCESS'){
                return $response;
            }
            $this->message = $response['return_msg'];
        }catch(\Exception $e){
            $this->message = $e->getMessage();
        }
        return false;
    }

    /**
     * 查询订单
     * @param array $options
     * @return array
     * @throws Exceptions\LocalCacheException
     * @throws InvalidResponseException
     */
    public function queryOrder($out_trade_no)
    {
        try {
            // 3. 创建接口实例
            $wechat = new \wxpay\Pay(config('wechat.'));
            // 4. 组装参数，可以参考官方商户文档
            $options = [
                // 'transaction_id' => '1008450740201411110005820873',
                'out_trade_no'   => $out_trade_no,
            ];
            $result = $wechat->queryOrder($options);
            if($result['return_code'] === 'SUCCESS' && $result['trade_state'] === 'SUCCESS'){
                return $result;
            }

            $this->message = $result['return_msg'];
        }catch(\Exception $e){
            $this->message = $e->getMessage();
        }
        return false;
    }

    /*
     * 显示二维码
     * @param string $url
     * @return \think\Response
     * @throws \Endroid\QrCode\Exceptions\ImageFunctionFailedException
     * @throws \Endroid\QrCode\Exceptions\ImageFunctionUnknownException
     * @throws \Endroid\QrCode\Exceptions\ImageTypeInvalidException
     */
    protected function qrcode($url)
    {
        $qrCode = new QrCode();
        $qrCode->setText($url)->setSize(300)->setPadding(20)->setImageType(QrCode::IMAGE_TYPE_PNG);
        return $qrCode->get();
    }
}
